package org.jypj.dev.controller;

import org.jypj.dev.model.CodeData;
import org.jypj.dev.model.Thing;
import org.jypj.dev.model.User;
import org.jypj.dev.service.ICodeDataService;
import org.jypj.dev.service.IThingProblemService;
import org.jypj.dev.service.IThingService;
import org.jypj.dev.service.IThingUseRecordService;
import org.jypj.dev.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenYu on 2017-03-06.
 * 物料管理控制器
 */
@Controller
@RequestMapping("thingController")
public class ThingController {

    @Resource
    private IThingService thingService;

    @Resource
    private ICodeDataService codeDataService;

    @Resource
    private IThingProblemService thingProblemService;

    /**
     * 跳转到物品列表页
     * 查询所有部门
     *
     * @return
     */
    @RequestMapping(value = "toThingList.action")
    public String toThingList(HttpServletRequest request) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("code", "bmgl");
        List<CodeData> codeDatas = codeDataService.selectAllByMap(queryMap);
        queryMap.put("code", "wpfl");
        List<CodeData> thingTypes = codeDataService.selectAllByMap(queryMap);
        request.setAttribute("codeDatas", codeDatas);
        request.setAttribute("thingTypes", thingTypes);
        return "back/thing/thing_main";
    }

    /**
     * 跳转到备品备件故障列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "toThingProblemList.action")
    public String toProblem181List(HttpServletRequest request) {


        return "back/thing/thing_problem_list";
    }

    /**
     * 分页查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectOnePageThingProblem.action")
    public Page selectOnePageThingProblem(Page page, String searchKey, String startDate) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("searchKey", searchKey);
        queryMap.put("startDate", startDate);
        page = thingProblemService.selectOnePageByMap(page, queryMap);
        return page;
    }

    /**
     * 分页查询车次
     *
     * @param page
     * @return
     */
    @RequestMapping("selectOnePage.action")
    @ResponseBody
    public Page selectOnePage(Page page, String searchKey, String currentNum) {
        Map<String, Object> map = new HashMap<>();
        map.put("searchKey", searchKey);
        map.put("currentNum", currentNum);
        page = thingService.selectOnePageByMap(page, map);
        return page;
    }


    /**
     * 保存或者更新
     *
     * @param thing
     * @return
     */
    @RequestMapping("saveOrUpdate.action")
    @ResponseBody
    public Map<String, Object> saveOrUpdate(Thing thing, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String userName = "";
        User user = (User) session.getAttribute("user");
        if (null != user) {
            userName = user.getUsername();
        }
        int code = 0;
        Date now = new Date();
        try {
            if (StringUtil.isNotEmpty(thing.getId())) {
                // 更新
                thing.setModifyUser(userName);
                thing.setModifyDate(sdf.format(now));
                thingService.updateThing(thing);
            } else {
                // 保存
                thing.setModifyUser(userName);
                thing.setCreateUser(userName);
                thing.setCreateDate(sdf.format(now));
                thing.setModifyDate(sdf.format(now));
                thingService.saveThing(thing);
            }
        } catch (Exception e) {
            code = -1;
            e.printStackTrace();
        }
        map.put("code", code);
        return map;
    }

    /**
     * 删除车次
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteThing.action")
    @ResponseBody
    public Map<String, Object> deleteTrain(String id) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            thingService.deleteThingById(id);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 根据id下载二维码
     *
     * @param id
     */
    @RequestMapping("downLoadQrCode.action")
    @ResponseBody
    public void downLoadQrCode(String id, HttpServletResponse response) {
        String filePath = PropertyUtil.getProperties("file_path") + "/qrCode/";
        String file_server_url = PropertyUtil.getProperties("file_server_url") + "/qrCode/";
        String logoPath = PropertyUtil.getProperties("file_server_url") + "/thingLogo/";
        Thing thing1 = thingService.selectThingById(id);
        String thingType = thing1.getType();
        if (StringUtil.isNotEmpty(thingType)) {
            logoPath = logoPath + thingType + ".png";
        }else {
            logoPath="";
        }
        String fileName = id + ".png";//生在在服务器本地硬盘的文件名
        String imageName = thing1.getName() + ".png";
        //先判断是否存在 若存在就不需要生成
        PathUtil.Path_Judge_Exist(filePath);
        File file = new File(filePath + fileName);
        String showContent = thing1.getName();
        TwoDimensionCode handler = new TwoDimensionCode();
        handler.encoderQRCode(id, showContent, logoPath, filePath + fileName, "png");
        ImageUtil.downloadImage(response, imageName, file_server_url + fileName);
    }
}
